package com.dayuanit.service.impl;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayuanit.dao.UserDao;
import com.dayuanit.domain.User;
import com.dayuanit.exception.MallException;
import com.dayuanit.service.UserService;
import com.dayuanit.utils.MailUtil;
import com.dayuanit.utils.UUIDUtil;
import com.dayuanit.vo.UserVO;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	private int row;
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public void addUser(UserVO vo) throws Exception {
		User userflag = userDao.getUser(vo.getUserName());
		if (null != userflag) {
			throw new MallException("用户已存在");
		}

		if (StringUtils.isBlank(vo.getUserName())) {
			throw new MallException("用户名不能为空");
		}

		if (!vo.getPassword().equals(vo.getPasswordRepeat())) {
			throw new MallException("两次输入的密码必须相等");
		}

		if (StringUtils.isBlank(vo.getPassword()) || StringUtils.isBlank(vo.getPasswordRepeat())) {
			throw new MallException("密码不能为空");
		}
		
		if (StringUtils.isBlank(vo.getSex())) {
			throw new MallException("性别不能为空");
		}
		
		if (StringUtils.isBlank(vo.getPhone())) {
			throw new MallException("手机号不能为空");
		}
		
		if (11 != vo.getPhone().length()) {
			throw new MallException("手机号必须为11位");
		}
		
		if (StringUtils.isBlank(vo.getMail())) {
			throw new MallException("邮箱不能为空");
		}
		
		if (!vo.getMail().contains("@") || !vo.getMail().contains(".")) {
			throw new MallException("邮箱格式有误");
		}
		
		User user = new User();
		String code = UUIDUtil.getUUID() + UUIDUtil.getUUID();
		user.setUserName(vo.getUserName());
    	user.setPassword(DigestUtils.md5Hex(vo.getPassword() + vo.getUserName()));
    	user.setSex(vo.getSex());
    	user.setPhone(vo.getPhone());
    	user.setMail(vo.getMail());
    	user.setCode(code);
    	user.setState(0);
		row = userDao.addUser(user);
		if (1 != row) {
			throw new MallException("注册失败");
		}
		MailUtil.sendMail(vo.getMail(), code);
	}
	
	@Override
	public User getUser(String userName) {
		return userDao.getUser(userName);
	}

	@Override
	public List<User> listUser(String userName) {
		return userDao.listUser(userName);
	}

	@Override
	public User getUserByCode(String code) {
		return userDao.getUserByCode(code);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean modifyState(User user) {
		return userDao.modifyState(user);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean modifyPassword(User user) {
		return userDao.modifyPassword(user);
	}

	@Override
	public User login(UserVO vo) throws Exception {
		
		if (StringUtils.isBlank(vo.getUserName())) {
			throw new MallException("用户名不能为空");
		}
		if (StringUtils.isBlank(vo.getPassword())) {
			throw new MallException("密码不能为空");
		}
		User user = userDao.getUser(vo.getUserName());
		if(null == user) {
			throw new MallException("用户不存在或密码错误");
		}
		String password = vo.getPassword() + vo.getUserName();
		if(!user.getPassword().equals(DigestUtils.md5Hex(password))) {
			throw new MallException("用户不存在或密码错误");
		}
		if (0 == user.getState()) {
			throw new MallException("该用户暂未激活");
		}
		return user;
	}

}
